package edu.gdkm.dao;

import edu.gdkm.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 用户数据访问接口
 */
public interface UserDao {
    
    /**
     * 插入用户
     */
    @Insert("INSERT INTO user(username, password, email, phone, user_type, avatar, status, create_time, update_time) VALUES(#{username}, #{password}, #{email}, #{phone}, #{userType}, #{avatar}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);
    
    /**
     * 根据ID删除用户
     */
    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteById(Long id);
    
    /**
     * 更新用户信息
     */
    @Update("UPDATE user SET username = #{username}, password = #{password}, email = #{email}, phone = #{phone}, user_type = #{userType}, avatar = #{avatar}, status = #{status}, update_time = #{updateTime} WHERE id = #{id}")
    int update(User user);
    
    /**
     * 根据ID查询用户
     */
    @Select("SELECT id, username, password, email, phone, user_type, avatar, status, create_time, update_time FROM user WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "email", column = "email"),
        @Result(property = "phone", column = "phone"),
        @Result(property = "userType", column = "user_type"),
        @Result(property = "avatar", column = "avatar"),
        @Result(property = "status", column = "status"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    User selectById(Long id);
    
    /**
     * 查询所有用户
     */
    @Select("SELECT id, username, password, email, phone, user_type, avatar, status, create_time, update_time FROM user")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "email", column = "email"),
        @Result(property = "phone", column = "phone"),
        @Result(property = "userType", column = "user_type"),
        @Result(property = "avatar", column = "avatar"),
        @Result(property = "status", column = "status"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<User> selectAll();
    
    /**
     * 根据用户名查询用户
     */
    @Select("SELECT id, username, password, email, phone, user_type, avatar, status, create_time, update_time FROM user WHERE username = #{username}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "email", column = "email"),
        @Result(property = "phone", column = "phone"),
        @Result(property = "userType", column = "user_type"),
        @Result(property = "avatar", column = "avatar"),
        @Result(property = "status", column = "status"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    User selectByUsername(String username);
    
    /**
     * 根据邮箱查询用户
     */
    @Select("SELECT id, username, password, email, phone, user_type, avatar, status, create_time, update_time FROM user WHERE email = #{email}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "email", column = "email"),
        @Result(property = "phone", column = "phone"),
        @Result(property = "userType", column = "user_type"),
        @Result(property = "avatar", column = "avatar"),
        @Result(property = "status", column = "status"),
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    User selectByEmail(String email);
}
